package com.zju.DoctorGo.controller;

import com.zju.DoctorGo.service.NoticeService;
import com.zju.DoctorGo.dto.NoticeFilterDTO;
import com.zju.DoctorGo.entity.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    private String normalizeField(String field) {
        if (field == null || field.equals("所有") || field.equals("全部")) {
            return null;
        }
        return field;
    }

    private String forReceiverFilter(String userType) {
        return switch (userType) {
            case "0" -> "";
            case "1" -> "1";
            case "2" -> "2";
            default -> userType;
        };
    }


    @PostMapping
    public ResponseEntity<?> getNotices(@RequestBody NoticeFilterDTO filter) {
        filter.setTitle(normalizeField(filter.getTitle()));
        filter.setDate(normalizeField(filter.getDate()));
        filter.setPriority(normalizeField(filter.getPriority()));
        filter.setSender(normalizeField(filter.getSender()));
        filter.setReceiver(forReceiverFilter(filter.getReceiver()));
        List<Notice> notices = noticeService.getNotices(filter);
        return ResponseEntity.ok(notices);
    }

    @PostMapping("/add")
    public ResponseEntity<?> addNotice(@RequestBody Notice notice) {
        noticeService.addNotice(notice);
        return ResponseEntity.ok().body("Notice sent successfully");
    }

    @PostMapping("/delete")
    public ResponseEntity<?> deleteNotice(@RequestBody List<Long> noticeIds) {
        noticeService.deleteNotices(noticeIds);
        return ResponseEntity.ok().body("Notices deleted successfully");
    }
}